﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WPLDB.Code;
using WPLDB.DA.Models;
using WPLDB.Utility.DB;
using WPLDB.Infrastucture.IO;
using WPLDB.Utility.Extensions;
using System.IO;
using System.Reflection;

namespace WPLDB.Test
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class CreateClassCodeTest
    {
        public CreateClassCodeTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }


        [TestMethod]
        public void GetConnectionString()
        {
            ConnectionModel model = new ConnectionModel
            {
                DataSource = @".\SQLEXPRESS",
                Mode = Authentication.Window
            };

            string connectionString = SQLHelper.GetConnectionString(model);
            Assert.IsFalse(string.IsNullOrEmpty(connectionString));

        }


        [TestMethod]
        public void GetDatabaseNames()
        {
            string connectionString = SQLHelper.GetConnectionString(new ConnectionModel
            {
                DataSource = @".\SQLEXPRESS",
                Mode = Authentication.Window
            });
            var listOfDatabase = SQLHelper.GetListOfDabase(connectionString);
            Assert.IsTrue(listOfDatabase.Count > 0);

        }

      

        [TestMethod]
        public void GetTableNamesForADatabase()
        {

            string connectionString = SQLHelper.GetConnectionString(new ConnectionModel
            {
                DataSource = @".\SQLEXPRESS",
                Mode = Authentication.Window,
                Database = "CO2E"
            });

            var listOfTables = SQLHelper.GetListOfTableNames(connectionString);
            Assert.IsTrue(listOfTables.Count > 0);

        }

      

        [TestMethod]
        public void CreateEntityClassCodeWithDBNameAndListOfTables()
        {
            string database = "CO2E";
            string connectionString = SQLHelper.GetConnectionString(new ConnectionModel
            {
                DataSource = @".\SQLEXPRESS",
                Mode = Authentication.Window,
                Database = database
            });

            var listOfTables = SQLHelper.GetListOfTableNames(connectionString);
            List<TableModel> listOfTableModel = new List<TableModel>();
            if (null != listOfTables)
                listOfTableModel = SQLHelper.GetListOfTableEntity(connectionString, listOfTables.ToList());

            CreateEntityClassCode createEntityClassCode = new CreateEntityClassCode(database + "DataContext", listOfTableModel);
            var str = createEntityClassCode.GenerateClassString();

            Assert.IsNotNull(str);


        }

        [TestMethod]
        public void CreateTableClassCodeWithTableModel()
        {

            string database = "CO2E";
            string connectionString = SQLHelper.GetConnectionString(new ConnectionModel
            {
                DataSource = @".\SQLEXPRESS",
                Mode = Authentication.Window,
                Database = database
            });

            var listOfTables = SQLHelper.GetListOfTableNames(connectionString);

            string str = string.Empty;

            if (null != listOfTables)
                foreach (var item in listOfTables)
                    str += (new CreateTableClassCode(SQLHelper.GetTable(connectionString, item, listOfTables.ToList()))).GenerateClassString();

            Assert.IsNotNull(str);

        }

        [TestMethod]
        public void CreateAllCode()
        {

            string database = "CO2E";
            string content = "";
            string connectionString = SQLHelper.GetConnectionString(new ConnectionModel
            {
                DataSource = @".\SQLEXPRESS",
                Mode = Authentication.Window,
                Database = database
            });

            var listOfTables = SQLHelper.GetListOfTableNames(connectionString);

            WPLDB.Utility.DB.SQLHelper.SetForeignKey(connectionString, listOfTables);
            List<TableModel> listOfTableModel = new List<TableModel>();
            if (null != listOfTables)
                listOfTableModel = SQLHelper.GetListOfTableEntity(connectionString, listOfTables.ToList());

            content += (new CreateTableBaseClassCode()).GenerateClassString();

            CreateEntityClassCode createEntityClassCode = new CreateEntityClassCode(database + "DataContext", listOfTableModel);
            content += createEntityClassCode.GenerateClassString();

            if (null != listOfTables)
                foreach (var item in listOfTables)
                    content += (new CreateTableClassCode(SQLHelper.GetTable(connectionString, item, listOfTables.ToList()))).GenerateClassString();



            content = (new CreateNameSpaceCode("CO2E.UI.WP7.Infrastructure.CO2EDB", content)).GenerateClassString();

            FileOperations fileOperations = new FileOperations();
            var fileName = fileOperations.Write(@"D:\temp\temp.cs", content, true);
            fileOperations.Delete(fileName);

            Assert.IsNotNull(content);


        }

        [TestMethod]
        public void CreateAllCodeForSelectedTables()
        {

            string database = "CO2E";
            string content = "";
            string contentHelper = "";
            List<TableModel> listOfTableModel = new List<TableModel>();
            string connectionString = SQLHelper.GetConnectionString(new ConnectionModel
            {
                DataSource = @".\SQLEXPRESS",
                Mode = Authentication.Window,
                Database = database
            });

            var listOfTables = SQLHelper.GetListOfTableNames(connectionString);

            int count = listOfTables.Count;

            for (int i = count - 1; i > 1; i--)
            {
                listOfTables.RemoveAt(i);
            }

            WPLDB.Utility.DB.SQLHelper.SetForeignKey(connectionString, listOfTables);

            content += (new CreateTableBaseClassCode()).GenerateClassString();

            if (null != listOfTables)
                listOfTableModel = SQLHelper.GetListOfTableEntity(connectionString, listOfTables.ToList());

            CreateEntityClassCode createEntityClassCode = new CreateEntityClassCode(database + "DataContext", listOfTableModel);
            content += createEntityClassCode.GenerateClassString();

           
            if (null != listOfTableModel)
                foreach (var item in listOfTableModel)
                    content += (new CreateTableClassCode(item)).GenerateClassString();

            CreateHelperClassCode createHelperClassCode = new CreateHelperClassCode(listOfTableModel, database, database + "DataContext", "CO2E.UI.WP7.Infrastructure.CO2EDB", "temp");

            contentHelper = createHelperClassCode.GenerateClassString();


            content = (new CreateNameSpaceCode("CO2E.UI.WP7.Infrastructure.CO2EDB", content)).GenerateClassString();

            FileOperations fileOperations = new FileOperations();
            var fileName = fileOperations.Write(@"D:\temp\temp.cs", content, true);
            var fileName1 = fileOperations.Write(@"D:\temp\tempHelper.cs", contentHelper, true);
            //    fileOperations.Delete(fileName);


            Assert.IsNotNull(content);


        }

        [TestMethod]
        public void RemoveSpecialChar()
        {
            string str = "abc-#$&^: ;*&DEF;";
            var outstr = str.RemoveSpecialChar();
            Assert.AreEqual(outstr, "abcDEF");
        }

        [TestMethod]
        public void ReplaceTestWithNullNull()
        {
            string str = null;
            var outstr = str.ReplaceClass(null);

            Assert.IsNull(outstr);
        }
        [TestMethod]
        public void ReplaceTestWithValueNull()
        {
            string str = "Sample";
            var outstr = str.ReplaceClass(null);
            Assert.AreEqual(str, outstr);
        }

        [TestMethod]
        public void GetLastProjectItemNameTest()
        {
            string str = "abc.one.three";
            var outstr = str.LastProjectItemName();
            Assert.AreEqual("three", outstr);

            str = "three";
            outstr = str.LastProjectItemName();
            Assert.AreEqual("three", outstr);
        }

        [TestMethod]
        public void CreateTempFolderTest()
        {
            FileOperations fileOperations = new FileOperations();
            fileOperations.CreateTempFolder(@"D:\temp");
        }


        [TestMethod]
        public void GetAssemblyInfoTest()
        {


            var ass = Assembly.Load("Microsoft.Practices.ServiceLocation");
            //"WPLDB.Code, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b51e984e4ed0a17c"
            //"WPLDB.DA.Interfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b51e984e4ed0a17c"
            //"WPLDB.DA.Models, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b51e984e4ed0a17c"
            //"WPLDB.Infrastucture, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b51e984e4ed0a17c"
            //"WPLDB.SQL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b51e984e4ed0a17c"
            //WPLDB.Utility
            //"Microsoft.Data.ConnectionUI.Dialog, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
            //"Microsoft.Data.ConnectionUI, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
            //"Microsoft.Practices.Prism, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            //Microsoft.Practices.Prism.MefExtensions
            //"Microsoft.Practices.ServiceLocation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            Assert.IsTrue(ass.ToString().Length > 0);

        }
    }
}
